import requests
import pandas as pd 
import numpy as np 
import json
from time import sleep
import random
hearders = {
    'Host': 'qun.qq.com',
    'Origin': 'http://qqweb.qq.com',
    'Cookie': 'skey=MI0SSv9Bdp; uin=o3201358688',
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15G77 QQ/7.8.2.435 V1_IPH_SQ_7.8.2_1_APP_A Pixel/1080 Core/UIWebView Device/Apple(iPhone 6Plus) NetType/WIFI QBWebViewType/1',
    'Accept-Language': 'zh-cn',
    # 'Referer':'http://qqweb.qq.com/m/relativegroup/index.html?_wv=4194305&_bid=165&source=qun_rec&keyword=%E6%88%B7%E5%A4%96',
    'Accept-Encoding': 'gzip, deflate',
}
url = 'http://qun.qq.com/cgi-bin/group_search/group_search?retype=2&keyword=%s&page=%d&wantnum=20&city_flag=0&distance=1&ver=1&from=9&bkn=803552150&style=1&lat=30450891&lon=114435341'
# 数据

city  = ["深圳","广州","成都","北京","上海","新疆"]

for  cityname  in  city:
    code = []
    name = []
    memo = []
    owner_uin = []
    try:
        for i in range(0,30):
            strr = cityname + "户外"
            url_batch = url % (strr,i)
            sleep(random.randint(3, 4))
            print(url_batch)
            resopon = requests.get(url_batch, headers=hearders)
            data = resopon.text
            json_data = json.loads(data)
            for item in json_data['group_list']:
                code.append(item['code'])
                name.append(item['name'])
                memo.append(item['memo'])
                owner_uin.append(item['owner_uin'])
    except:
        data = pd.DataFrame()
        data['code'] = code
        data['name'] = name
        data['memo'] = memo
        data['owner_uin'] = owner_uin
        city_csv = './%s_data.csv'%cityname
        data.to_csv(city_csv)